var tiRequest;

/**
*	--- Init Methods ---
*/
window.onload = function() {
	init();
}

window.onresize = function() {
	setHeights();
}

window.onfocus = function() {
}

function init() {
	getArtistsByLetter('A');
	setHeights();
	setSearchResultPositions();
}

function setHeights() {
	//Make sure all the divs lign up
	var artistContainer = document.getElementById('artistContainer');
	var albumsContainer = document.getElementById('albumsContainer');
	var albumContainer = document.getElementById('albumContainer');
	
	var top = findPosY(artistContainer);
	
	albumsContainer.style.top = top;
	albumContainer.style.top = top;

	var windowHeight = document.body.clientHeight;
	artistContainer.style.height = windowHeight-top-5;
	albumsContainer.style.height = windowHeight-top-5;
	albumContainer.style.height = windowHeight-top-5;
}

/**
*	--- Get albums by name
*/
function getAlbumsByNameProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseText;
			processAlbumsByArtistResponse(response);
			tiRequest = null;
		}
	}
}

function getAlbumsByName(name) {
	var albumContainer = document.getElementById('albumContainer');
	var artistContainer = document.getElementById('artistContainer');
	
	artistContainer.innerHTML = "";
	albumsContainer.innerHTML = "<div class='message'>Loading albums for "+name+"...</div>";
	albumContainer.innerHTML = "";
	
	var url = 'albumSearcyByName.htm';
	var parameters = 'name='+name;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = albumsByArtistProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore
	}
}

function processgetAlbumsByNameResponse(response) {
	document.getElementById('albumsContainer').innerHTML = response;
}

/**
*	--- Get album by ID ---
*/
function albumByIdProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseText;
			processAlbumByIdResponse(response);
			tiRequest = null;
		}
	}
}

function getAlbumById(albumId) {
	var albumContainer = document.getElementById('albumContainer');
	var artistInfoContainer = document.getElementById('artistInfo');
	
	albumContainer.innerHTML = "<div class='message'>Loading album named "+albumId+"...</div>";
	
	var url = 'album.htm';
	var parameters = 'albumId='+albumId;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = albumByIdProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore.
	}
}

function processAlbumByIdResponse(response) {
	var albumContainer = document.getElementById('albumContainer');
	albumContainer.innerHTML = response;
}

/**
*	--- Get albums by  artist ID ---
*/
function albumsByArtistIdProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseXML.documentElement;
			processAlbumsByArtistIdResponse(response);
			tiRequest = null;
		}
	}
}

function getAlbumsByArtistId(artistId) {
	var albumContainer = document.getElementById('albumContainer');
	var albumsContainer = document.getElementById('albumsContainer');
	albumsContainer.innerHTML = "<div class='message'>Loading albums for artist ID"+artistId+"...</div>";
	albumContainer.innerHTML = "";


	var url = 'albumList.htm';
	var parameters = 'artistId='+artistId;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = albumsByArtistIdProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore
	}
}

function processAlbumsByArtistIdResponse(response) {
	var albumsContainer = document.getElementById('albumsContainer');
	var artistId = response.getAttribute('artistId');
	var artistName = response.getAttribute('artistName');
	var albums = response.getElementsByTagName('album');
	var html = "";
	
	for(var i=0;i < albums.length;i++) {
		var albumId = getObjProperty(albums[i], 'id');
		var albumName = getObjProperty(albums[i], 'name');
		html += "<div class=\"row"+i%2+"\"/>";
		html += "<a href='javascript:getAlbumById(\""+albumId+"\");'>"+albumName+"</a>";
		html += "</div>";
	}
	
	if(albums.length == 0) {
		html = "No albums found for this artist";
	}
	
	albumsContainer.innerHTML = html;
}

/**
*	--- Get artists by letter --
*/
function getArtistsByLetter(letter) {
	document.getElementById('artistContainer').innerHTML = "<div class='message'>Loading artists for letter "+letter+"...</div>";
	document.getElementById('albumContainer').innerHTML = "";
	document.getElementById('albumsContainer').innerHTML = "";
	
	var url = 'artistList.htm';
	var parameters = 'letter='+letter;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = artistByLetterProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore.
	}
}

function artistByLetterProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseText;
			processArtistByLetter(response);
			tiRequest = null;
		}
	}
}

function processArtistByLetter(response) {
	document.getElementById('artistContainer').innerHTML = response;
}

/**
*	--- Get artist by Id --
*/
function getArtistById(artistId) {
	document.getElementById('artistContainer').innerHTML = "<div class='message'>Loading artis for ID "+artistId+"...</div>";
	document.getElementById('albumContainer').innerHTML = "";
	document.getElementById('albumsContainer').innerHTML = "";
	
	var url = 'artistList.htm';
	var parameters = 'artistId='+artistId;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = artistByIdProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore.
	}
}

function artistByIdProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseText;
			processArtistById(response);
			tiRequest = null;
		}
	}
}

function processArtistById(response) {
	var artistContainer =  document.getElementById('artistContainer');
	artistContainer.innerHTML = response;
}
/**
*	--- Get Artists by Name ---
*/
function processArtistsByName(response) {
	var artistSearchResults = document.getElementById('artistSearchResults');
	artistSearchResults.innerHTML = response;
}

function getArtistsByName(name) {
	var artistSearchResults = document.getElementById('artistSearchResults');
	artistSearchResults.style.display = "inline";
	artistSearchResults.innerHTML = "Searching for '"+name+"' ...";
	
	var url = 'artistList.htm';
	var parameters = 'name='+name;
	tiRequest = getXMLRequestObj();
	tiRequest.onreadystatechange = artistsByNameProcess;
	tiRequest.open("POST", url, true);
	tiRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	tiRequest.setRequestHeader("Content-length", parameters.length);
	tiRequest.setRequestHeader("Connection", "close");
	tiRequest.send(parameters);

	try {
		tiRequest.send(null); //safari/firefox
	} catch (e) {
		//Just ignore.
	}
}

function artistsByNameProcess() {
	if(tiRequest.readyState == 4) {
		if(tiRequest.status == 200) {
			var response = tiRequest.responseText;
			processArtistsByName(response);
			tiRequest = null;
		}
	}
}


/**
*	--- mp3 playing functions --
*/
function openSong(songId, artistName) {
	var link = document.getElementById(songId);	
	var songURL = link.href;
	var songName = link.text;	
	
	wimpy_appendPlaylist('track', songURL, artistName, songName, songURL, "");
	return;
}

function playAlbum(songCount, artistName) {
	var songsToAdd = "";
	for(var i=0;i<songCount;i++) {
		var link = document.getElementById("song"+i);
		var songURL = link.href;
		songsToAdd += songURL+"|";
	}
	
	songsToAdd = songsToAdd.substr(0,songsToAdd.length-1);	
	wimpy_appendMultipleTracks(songsToAdd, artistName, "", "");
}

/**
*	--- Artists search DHTML functions --
*/
function setSearchResultPositions() {
	var artistSearchResults = document.getElementById('artistSearchResults');
	var artistSearch = document.getElementById('artistSearch');
	artistSearchResults.style.top = findPosY(artistSearch)+22;
	artistSearchResults.style.left = findPosX(artistSearch);
}

function artistSearchFocus(input) {
	if(input.value == 'Artist Search') {
		input.value = '';
	} else {
		getArtistsByName(input.value);
	}
}

function artistSearchResultOver(row) {
		row.style.backgroundColor = "#B0B0B0";
		row.style.cursor = "default";
}

function artistSearchResultOut(row) {
		row.style.backgroundColor = "white";
}

function artistSearchResultClick(artistId, artistName) {
		getAlbumsByArtistId(artistId);
		
		var artistSearchResults = document.getElementById('artistSearchResults');
		var artistSearch = document.getElementById('artistSearch');
		artistSearchResults.style.display = 'none';
		artistSearch.value = 'Artist Search';

		var artistContainer = document.getElementById('artistContainer');
		var html = "<div id=\"artist1\" class=\"row1\">";
			html +="<a href='javascript:getAlbumsByArtistId(\""+artistId+"\");'>"+artistName+"</a>";
			html +="</div>";
			
		artistContainer.innerHTML = html;
}

/**
*	--- Album search DHTML functions ---
*/
function albumSearchFocus(input) {
	if(input.value == 'Album Search') {
		input.value = '';
	} else {
		getArtistsByName(input.value);
	}
}

function albumSearchResultOver(row) {
		row.style.backgroundColor = "#B0B0B0";
		row.style.cursor = "default";
}

function albumSearchResultOut(row) {
		row.style.backgroundColor = "white";
}

function albumSearchResultClick(albumId) {
		getAlbumsByArtistId(albumId);
		var artistSearchResults = document.getElementById('artistSearchResults');
		var artistSearch = document.getElementById('artistSearch');
		artistSearchResults.style.display = 'none';
		artistSearch.value = 'Artist Search';
}